#include "Main.h"

ScriptData* FindScript(DWORD dwScriptId, CHAR* szScriptPath)
{
	JSContext *jsContext = NULL;

	if(dwScriptId) // If ScriptId is defined, return the ScriptData with the matchin id!
	{
		for(INT i = 0; i < Scripts.GetSize(); i++)
		{
			ScriptData* js = Scripts.ElementAt(i);

			if(!js)
				continue;

			if(js->GetScriptId() == dwScriptId)
				return js;
		}

		return NULL;
	}

	if(szScriptPath)
	{
		for(INT i = 0; i < Scripts.GetSize(); i++)
		{
			ScriptData* js = Scripts.ElementAt(i);

			if(!js)
				continue;

			if(strstr(js->GetFile(), szScriptPath))
				return js;
		}

		return NULL;
	}

	jsContext = NULL;

	for(INT i = 0; i < Scripts.GetSize(); i++)
	{
		ScriptData* js = Scripts.ElementAt(i);

		if(!js)
			continue;

		return js;
	}

	return NULL;
}

ScriptData* FindNextScript(DWORD dwScriptId, CHAR* szScriptPath)
{
		JSContext *jsContext = NULL;

		if(dwScriptId)
		{
			ScriptData* pjs = FindScript(dwScriptId, NULL);
		
			BOOL bFound = FALSE;

			if(pjs)
				for(INT i = 0; i < Scripts.GetSize(); i++)
				{
					ScriptData* js = Scripts.ElementAt(i);

					if(!js)
						continue;

					if(!bFound && pjs->GetScriptId() == js->GetScriptId())
					{
						bFound = TRUE;
						continue;
					}

					if(bFound)
						return js;
				}

			return NULL;
		}

		if(szScriptPath && szScriptPath[0])
		{
			ScriptData* pjs = FindScript(dwScriptId, NULL);
		
			BOOL bFound = FALSE;

			if(pjs)
				for(INT i = 0; i < Scripts.GetSize(); i++)
				{
					ScriptData* js = Scripts.ElementAt(i);

					if(!js)
						continue;

					if(!bFound && pjs->GetScriptId() == js->GetScriptId())
					{
						bFound = TRUE;
						continue;
					}

					if(bFound)
						if(strstr(js->GetFile(), szScriptPath))
							return js;
				}
		}

		return NULL;
}